﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using GIMS.Def.Arguments;
using GIMS.Def.Operators;
using GIMS.Def.Settings;

namespace GIMS.GUI.GeneralConfig
{
    public class GeneralConfigWindowViewModel
    {
        private IEnumerable<Setting> settings;
        private Argument argument;

        public GeneralConfigWindowViewModel(Operator imOperator)
        {
            settings = imOperator.Settings;
            argument = imOperator.Argument;
            InitArgumentControls(imOperator.Name, imOperator.Description);
        }

        public GeneralConfigWindowViewModel(GlobalSettings globalSettings)
        {
            settings = GlobalSettings.Instance.Settings;
            InitArgumentControls();
        }

        private void InitArgumentControls(string argumentName = null, string argumentDescription = null)
        {
            if (HasArgument)
            {
                ArgumentAsUiSetting = new ArgumentRepresenter(argument, argumentName, argumentDescription);
            }

            if (HasSettings)
            {
                SettingsAsUiElements = new ObservableCollection<SettingRepresenter>();
                foreach (var setting in settings)
                {
                    SettingsAsUiElements.Add(new SettingRepresenter(setting));
                }
            }
        }

        public bool HasArgument
        {
            get { return null != argument; }
        }

        public bool HasSettings
        {
            get
            {
                return null != settings
                       && settings.Any();
            }
        }

        public ArgumentRepresenter ArgumentAsUiSetting
        {
            get;
            private set;
        }

        public ObservableCollection<SettingRepresenter> SettingsAsUiElements
        {
            get;
            private set;
        }
    }
}
